<?php

class ServicesController extends AppController {
    
    // define controller name
    var $name = 'Services';
    
    // define list of models to use 
    var $uses = array('Service');
    
    // set controller domain
    var $controllerDomain = 'services';
    
    public function beforeFilter() {
        parent::beforeFilter();
        
    }
    
    public function isAuthorized() {
        
        $valid = parent::isAuthorized();
        if ($valid == false) {
            return false;
        }
        
        return true;
    }
    
    // create a new user
    public function create() {
        
        // get service types
        $this->getServiceTypes();
        
        if ($this->request->is('post')) {
            
            if ($this->Service->sanitizeAll() && $this->Service->validates() && $this->Service->save($this->request->data)) {                
                $this->Session->setFlash(__('Service created',true));
                $this->redirect(array('controller' => 'services','action' => 'index'));              
            }
            
            $this->Session->setFlash(__('Please correct the mistakes below',true));
        }
    }
    
    public function delete($id = null) {
        
        if (!$this->Service->activeRecordExists($id)) {   
            $this->Session->setFlash(__('Invalid request',true));
            $this->redirect(array('controller' => 'services','action' => 'index'));           
        }
        
        $this->Service->delete($id);
        $this->Session->setFlash(__('Service deleted',true));
        $this->redirect(array('controller' => 'services','action' => 'index'));
    }
    
    public function edit($id = null) {
       
        // chek that record exists
        if (!$this->Service->activeRecordExists($id)) {
            $this->Session->setFlash(__('Invalid request',true));
            $this->redirect(array('controller' => 'services','action' => 'index'));
        }
        
        // get db record
        $dbService = $this->Service->find('first',array('conditions' => array('id' => $id)));
        
        // get service types
        $this->getServiceTypes();
        
        // set data for modifications
        if ($this->request->is('get')) {
            $this->request->data = $dbService;
        }
        
        // process modifications
        if ($this->request->is('post')) {
            
            $this->Service->create();
            $this->Service->id = $id;
            
            if ($this->Service->sanitizeAll() && $this->Service->validates() && $this->Service->save($this->request->data)) { 
                $this->Session->setFlash(__('Service updated',true));
                $this->redirect(array('controller' => 'services','action' => 'index'));              
            }
            
            $this->Session->setFlash(__('Please correct the mistakes below',true));
        } 
    }
    
    public function view($id = null) {
        //todo
    }
    
    public function index() {
        //todo
    }
    
    public function get_items() {
        //todo
    }
    
    private function getServiceTypes($set = true, $persist = true) {
        
        if ($set) $this->set('serviceTypes',Constants::$service_types);
        if ($persist) $this->serviceTypes = Constants::$service_types;
        
        return Constants::$service_types;
    }
    
}


?>
